﻿/*
xPlayer v1.0
特性:
	1.文件大小 9.78kb;
	2.可以自定义皮肤;
	3.支持 http 和 rtmp 视频;
	4.音量调节,全屏播放,拖拽播放;
作者:胡俊
邮箱:whuthj@163.com;
Code license:GNU Lesser GPL;
说明:
	由于 Flex 自带视频播放器体积过于笨重,
	所以自己开发了一个轻量级的视频播放器 xPlayer 希望对大家有用,
	本源码可以随意传播或者修改完善,旨在方便开发者使用.
*/
package com.xplayer.utils
{
    public class XMLUtils extends Object
    {
        public static function toObject(xml:XML, isSetNamespace:Boolean = true) : Object
        {
            var obj:Object;
            if (xml)
            {
                obj = {};
                xml.ignoreWhitespace = true;
                pNode(xml, obj, isSetNamespace);
                return obj;
            }
            return null;
        }

        private static function pNode(xml:XML, obj:Object, isSetNamespace:Boolean) : void
        {
            var i:int = 0;
            if (isSetNamespace)
            {
                xml.setNamespace("");
            }
            if (!xml.name())
            {
                obj = null;
                
            }
            var lbl:String = xml.name().toString();
            var obj1:Object = {};
            if (xml.attributes().length() > 0)
            {
                i = 0;
                while (i < xml.attributes().length())
                {
                    
                    obj1[xml.attributes()[i].name().toString()] = xml.attributes()[i];
                    i++;
                }
                if (xml.children().length() <= 1 && obj1["value"] == undefined)
                {
                    obj1["value"] = xml.toString();
                }
            }
            else if (xml.children().length() <= 1 && !xml.hasComplexContent())
            {
                obj1 = xml.toString();
            }
            if (obj[lbl] == undefined)
            {
                obj[lbl] = obj1;
            }
            else if (obj[lbl] is Array)
            {
                obj[lbl].push(obj1);
            }
            else
            {
                obj[lbl] = [obj[lbl], obj1];
            }
            try
            {
                toObj(xml, obj[lbl], isSetNamespace);
            }
            catch (e:Error)
            {
            }
            
        }

        private static function toObj(xml:XML, obj:Object, isSetNamespace:Boolean) : void
        {
            var n1:int = 0;
            var n2:int = 0;
            var xml1:XML;
            n2 = xml.children().length();
            n1 = 0;
            while (n1 < n2)
            {
                xml1 = xml.children()[n1];
                if (obj is Array)
                {
                    pNode(xml1, obj[(obj.length - 1)], isSetNamespace);
                }
                else
                {
                    pNode(xml1, obj, isSetNamespace);
                }
                n1++;
            }
            
        }

    }
}
